﻿using System;
using System.Collections.Generic;
using System.Drawing;
using DevExpress.XtraEditors;
using CosyTrade.UI;
using DevExpress.XtraPrintingLinks;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraPrinting;
using CosyTrade.Store;

namespace CosyTrade.ClientForm.Printer
{
    public partial class PrintProducts : FormBase
    {
        private CompositeLink composLink;
 
        public delegate void LoadContentCallBack();

        public PrintProducts()
        {
            InitializeComponent();
        }

        private void checkEdit5_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in gridView1.Columns )//advBandedGridView1.Columns
                if (column.Name.Equals(((CheckEdit)sender).Tag))
                    column.Visible = !((CheckEdit)sender).Checked;
        }

        void LoadContent()
        {
            composLink = new CompositeLink(printBarManager1.PrintControl.PrintingSystem);

            composLink.CreateMarginalHeaderArea += new CreateAreaEventHandler(composLink_CreateMarginalHeaderArea);
            composLink.CreateMarginalFooterArea += new CreateAreaEventHandler(composLink_CreateMarginalFooterArea);
            PrintableComponentLink pcLink1 = new PrintableComponentLink();
            Link linkGrid1Report = new Link();
            linkGrid1Report.CreateDetailArea += new CreateAreaEventHandler(LinkGrid1ReportCreateDetailArea);

            pcLink1.Component = this.gridControl1;

            composLink.Links.Add(linkGrid1Report);
            composLink.Links.Add(pcLink1);

            composLink.CreateDocument();
        }

        void composLink_CreateMarginalFooterArea(object sender, CreateAreaEventArgs e)
        {
            string format = "Pag: {0} / {1}";
            e.Graph.Font = e.Graph.DefaultFont;
            e.Graph.BackColor = Color.Transparent;

            e.Graph.Font = new Font("Arial", 10, FontStyle.Regular);
            RectangleF r = new RectangleF(e.Graph.ClientPageSize.Width - 150, 0, 0, e.Graph.Font.Height);
            PageInfoBrick brick = e.Graph.DrawPageInfo(PageInfo.NumberOfTotal, format, Color.Black, r, DevExpress.XtraPrinting.BorderSide.None);
            brick.Alignment = BrickAlignment.Far;
            brick.AutoWidth = true;
        }

        void composLink_CreateMarginalHeaderArea(object sender, CreateAreaEventArgs e)
        {
            e.Graph.BorderWidth = 0;
            e.Graph.Font = new Font("黑体", 18, FontStyle.Bold);
            SizeF sf = PrintHelper.GetSizeF("商品资料详细列表", e.Graph.Font);
            float prvHeight = (60 - sf.Height) / 2;
            e.Graph.DrawString("商品资料详细列表", new RectangleF((e.Graph.ClientPageSize.Width - sf.Width) / 2, prvHeight, sf.Width + 10, sf.Height));
            prvHeight += sf.Height + 10;
            e.Graph.Font = new Font("黑体", 14, FontStyle.Regular);
            sf = PrintHelper.GetSizeF(DateTime.Now.ToString("yyyy/MM/dd"), e.Graph.Font);
            e.Graph.DrawString(DateTime.Now.ToString("yyyy/MM/dd"), new RectangleF((e.Graph.ClientPageSize.Width - sf.Width) / 2, prvHeight + 5, sf.Width, sf.Height));
        }

        static void LinkGrid1ReportCreateDetailArea(object sender, CreateAreaEventArgs e)
        {
            e.Graph.Font = new Font("Arial", 12);
            e.Graph.BorderWidth = 0;
            e.Graph.DrawRect(new RectangleF(0, 0, e.Graph.ClientPageSize.Width, 30), DevExpress.XtraPrinting.BorderSide.Bottom, Color.Silver, Color.Transparent);
            e.Graph.BackColor = Color.Transparent;
            e.Graph.DrawString("商品资料", new RectangleF(5, 5, 150, 25));
            e.Graph.Font = new Font("Arial", 8, FontStyle.Bold);

            //e.Graph.DrawString("进口单号: " + txtExportBillNumber.Text, new RectangleF(0, 32, e.Graph.ClientPageSize.Width * (float)0.25, 25));
            //e.Graph.DrawString("总件数: " + txtPackageCount.Text, new RectangleF(0, 57, e.Graph.ClientPageSize.Width * (float)0.25, 25));
            //e.Graph.DrawString("美金单价: " + txtTotal.Text, new RectangleF(0, 82, e.Graph.ClientPageSize.Width * (float)0.25, 25));

            //e.Graph.DrawString("收货日期: " + dtCargoDate.Text, new RectangleF(e.Graph.ClientPageSize.Width * (float)0.25, 32, e.Graph.ClientPageSize.Width * (float)0.25, 25));
            //e.Graph.DrawString("商品总数: " + txtTotalCount.Text, new RectangleF(e.Graph.ClientPageSize.Width * (float)0.25, 57, e.Graph.ClientPageSize.Width * (float)0.25, 25));
            //e.Graph.DrawString("委币单价: " + txtTotal2.Text, new RectangleF(e.Graph.ClientPageSize.Width * (float)0.25, 82, e.Graph.ClientPageSize.Width * (float)0.25, 25));

            //e.Graph.DrawString("签收人: " + txtReceiveEmployee.Text, new RectangleF(e.Graph.ClientPageSize.Width * (float)0.5, 32, e.Graph.ClientPageSize.Width * (float)0.5, 25));
            //e.Graph.DrawString("点货日期: " + dtTallyingDate.Text, new RectangleF(e.Graph.ClientPageSize.Width * (float)0.5, 57, e.Graph.ClientPageSize.Width * (float)0.25, 25));

            //e.Graph.DrawString("入口汇率: " + txtExchange.Text, new RectangleF(e.Graph.ClientPageSize.Width * (float)0.75, 32, e.Graph.ClientPageSize.Width * (float)0.25, 25));
            //e.Graph.DrawString("点货员工: " + txtTallyingEmployee.Text, new RectangleF(e.Graph.ClientPageSize.Width * (float)0.75, 57, e.Graph.ClientPageSize.Width * (float)0.25, 25));

            //e.Graph.Font = new Font("Arial", 12);
            //e.Graph.DrawRect(new RectangleF(0, 105, e.Graph.ClientPageSize.Width, 30), BorderSide.Bottom, Color.Silver, Color.Transparent);
            //e.Graph.BackColor = Color.Transparent;
            //e.Graph.DrawString("进出口差价", new RectangleF(5, 110, e.Graph.ClientPageSize.Width * (float)0.25, 25));

            //e.Graph.Font = new Font("Arial", 8, FontStyle.Bold);
            //e.Graph.DrawString("总装箱数: " + txtDPackageCount.Text, new RectangleF(0, 135, e.Graph.ClientPageSize.Width * (float)0.25, 25));
            //e.Graph.DrawString("商品总件数: " + txtDProductCount.Text, new RectangleF(e.Graph.ClientPageSize.Width * (float)0.25, 135, e.Graph.ClientPageSize.Width * (float)0.25, 25));
            //e.Graph.DrawString("美金总差: " + txtDTotal.Text, new RectangleF(e.Graph.ClientPageSize.Width * (float)0.5, 135, e.Graph.ClientPageSize.Width * (float)0.25, 25));
            //e.Graph.DrawString("委币总差: " + txtDTotal2.Text, new RectangleF(e.Graph.ClientPageSize.Width * (float)0.75, 135, e.Graph.ClientPageSize.Width * (float)0.25, 25));
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (composLink != null) composLink.CreateDocument();
        }

        private void PrintProducts_Load(object sender, EventArgs e)
        {
            if (ProductSources != null)
                bindingSource1.DataSource = ProductSources;
            LoadContent();
        }

        private IList<Product> productSources = null;
        public IList<Product> ProductSources
        {
            set { productSources = value; }
            get { return productSources; }
        }

    }
}